/**
 * Settle模块 - 结算管理API
 * @description 结算管理相关的API接口定义
 * @author AI Assistant
 * @since 1.0.0
 */

import request from '@/config/axios'
import type { SettleOrderVO, SettleOrderPageReqVO, PageResult } from './types'

/**
 * 获取结算单分页列表
 * @param params 分页查询参数
 * @returns 结算单分页数据
 */
export const getSettleOrderPageApi = (
  params: SettleOrderPageReqVO
): Promise<PageResult<SettleOrderVO>> => {
  return request.get({ url: '/settle/settle-order/page', params })
}

/**
 * 获取结算单详情
 * @param id 结算单ID
 * @returns 结算单详情
 */
export const getSettleOrderApi = (id: number): Promise<SettleOrderVO> => {
  return request.get({ url: `/settle/settle-order/get?id=${id}` })
}

/**
 * 手动创建结算单
 * @param merchantNo 商户编号
 * @param settleCycle 结算周期
 * @returns 创建结果
 */
export const createSettleOrderApi = (merchantNo: string, settleCycle: number): Promise<number> => {
  return request.post({
    url: '/settle/settle-order/create',
    data: { merchantNo, settleCycle }
  })
}

/**
 * 执行结算
 * @param id 结算单ID
 * @returns 结算结果
 */
export const executeSettleApi = (id: number): Promise<boolean> => {
  return request.post({ url: `/settle/settle-order/execute?id=${id}` })
}

/**
 * 批量执行结算
 * @param ids 结算单ID列表
 * @returns 批量结算结果
 */
export const batchExecuteSettleApi = (ids: number[]): Promise<boolean> => {
  return request.post({
    url: '/settle/settle-order/batch-execute',
    data: { ids }
  })
}

/**
 * 取消结算
 * @param id 结算单ID
 * @param remark 取消原因
 * @returns 取消结果
 */
export const cancelSettleApi = (id: number, remark?: string): Promise<boolean> => {
  return request.put({
    url: '/settle/settle-order/cancel',
    data: { id, remark }
  })
}

/**
 * 重新结算
 * @param id 结算单ID
 * @returns 重新结算结果
 */
export const retrySettleApi = (id: number): Promise<boolean> => {
  return request.post({ url: `/settle/settle-order/retry?id=${id}` })
}

/**
 * 获取结算统计数据
 * @param merchantNo 商户编号(可选)
 * @param dateRange 日期范围(可选)
 * @returns 统计数据
 */
export const getSettleStatisticsApi = (params: {
  merchantNo?: string
  dateRange?: [string, string]
}): Promise<{
  totalAmount: number // 总结算金额
  totalCount: number // 总结算笔数
  pendingAmount: number // 待结算金额
  pendingCount: number // 待结算笔数
  settledAmount: number // 已结算金额
  settledCount: number // 已结算笔数
  failedAmount: number // 结算失败金额
  failedCount: number // 结算失败笔数
}> => {
  return request.get({ url: '/settle/settle-order/statistics', params })
}

/**
 * 导出结算单数据
 * @param params 查询参数
 * @returns 导出文件
 */
export const exportSettleOrderApi = (params: SettleOrderPageReqVO): Promise<void> => {
  return request.download({
    url: '/settle/settle-order/export',
    params
  })
}

/**
 * 下载结算单详情
 * @param id 结算单ID
 * @returns 下载文件
 */
export const downloadSettleOrderDetailApi = (id: number): Promise<void> => {
  return request.download({
    url: `/settle/settle-order/download-detail?id=${id}`
  })
}
